package com.resou.reader.signin;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.resou.reader.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 补签对话框
 */
public class SupplementDialog extends DialogFragment {

    //显示对话的类型
    //分享
    public static final int SHARE = 1;
    //充值
    public static final int RECHARGE = 2;
    public static final String WEEK_DAY = "weekDay";
    public static final String SHOW_TYPE = "showType";

    @BindView(R.id.iv_recharge_image)
    ImageView ivRechargeImage;
    @BindView(R.id.tv_recharge_button)
    TextView tvRechargeButton;
    Unbinder unbinder;
    @BindView(R.id.tv_info)
    TextView tvInfo;
    private int mWeekDay;
    private int mShowType;

    public static SupplementDialog newInstance(int weekDay, int showType) {
        Bundle args = new Bundle();
        args.putInt(WEEK_DAY, weekDay);
        args.putInt(SHOW_TYPE, showType);
        SupplementDialog fragment = new SupplementDialog();
        fragment.setArguments(args);
        return fragment;
    }

    private OnSupplementDialogInteractionListener mInteractionListener;

    public void setInteractionListener(OnSupplementDialogInteractionListener interactionListener) {
        mInteractionListener = interactionListener;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mWeekDay = getArguments().getInt(WEEK_DAY);
            mShowType = getArguments().getInt(SHOW_TYPE);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_supplement, container, false);
        unbinder = ButterKnife.bind(this, view);
        Window window = getDialog().getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            params.windowAnimations = R.style.bottomSheet_animation;
//            window.getAttributes().windowAnimations = R.style.PauseDialogAnimation;
            getDialog().getWindow().setAttributes(params);
            getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            getDialog().setCanceledOnTouchOutside(true);
            getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
        if (mShowType == SHARE) {
            tvInfo.setText("分享给好友，即可获得补签机会");
            tvRechargeButton.setText("立即分享");
            tvRechargeButton.setOnClickListener(v -> {
                mInteractionListener.onShareClicked(mWeekDay);
                //分享
            });
        }
        if (mShowType == RECHARGE) {
            tvInfo.setText("充值即可获得补签机会");
            tvRechargeButton.setText("立即充值");
            tvRechargeButton.setOnClickListener(v -> {
                mInteractionListener.onRechargeClicked(mWeekDay);
                Log.d(TAG, "onCreateView: " + mWeekDay);
                //分享
            });
        }

        return view;
    }

    private static final String TAG = "SupplementDialog";
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void show(FragmentManager manager, String tag) {
        try {
            super.show(manager, tag);
        } catch (IllegalStateException ignore) {
            //  容错处理,不做操作
        }
    }

    public interface OnSupplementDialogInteractionListener {
        //充值点击后的回调
        void onRechargeClicked(int weekDay);

        void onShareClicked(int weekDay);
    }

}
